﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zocono.Infrastructure.Common
{
    public static class JsonConfigurationHelper
    {
        /// <summary>
        /// 读取配置文件的信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName">要读取json的名称</param>
        /// <param name="key">要读取的json节点名称</param>
        /// <returns></returns>    
        public static T GetAppSettings<T>(string fileName, string key) where T : class, new()
        {
            //获取根目录的路径
            var url = Directory.GetCurrentDirectory();
            IConfiguration config = new ConfigurationBuilder()
                       .SetBasePath(url)
                    .Add(new JsonConfigurationSource
                    {
                        Path = fileName,
                        Optional = true,
                        ReloadOnChange = true
                    })
                    .Build();

            var appconfig = config.GetSection(key).Get<T>();

            //IConfiguration config = new ConfigurationBinder()
            //    .SetBasePath(url)
            //    .Add(new JsonConfigurationSource { Path = fileName, Optional = false, ReloadOnChange = true })
            //    .Build();
            //var appconfig = new ServiceCollection()
            //.AddOptions()
            //.Configure<T>(configureOptions)
            //.BuildServiceProvider()
            //.GetService<IOptions<T>>()
            //.Value;

            return appconfig;
        }

        public static string GetJson(string jsonPath, string key)
        {
            IConfiguration config = new ConfigurationBuilder().AddJsonFile(jsonPath, false, true).Build(); //json文件地址
            string str = config.GetSection(key).Value; //json某个对象 
            return str;
        }

        public static void SetJson(string jsonPath, string key, string value)
        {
            IConfiguration config = new ConfigurationBuilder().AddJsonFile(jsonPath, false, true).Build(); //json文件地址
            config[key] = value;
        }

    }
}
